<!-- 购物车页面 -->
 <template>
  <div class="cart-page">
    <el-button class="return-button" @click="$router.back()" icon="ArrowLeft">返回</el-button>
    <h2>我的购物车</h2>
    <el-table :data="cartItems" style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="单价"></el-table-column>
      <el-table-column label="数量">
        <template #default="scope">
          <el-input-number v-model="scope.row.quantity" @change="updateCartItemQuantity(scope.row.id, $event)" :min="1" :max="10"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="小计">
        <template #default="scope">
          {{ scope.row.price * scope.row.quantity }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" type="danger" @click="removeFromCart(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="cart-total">
      <span>总价：{{ totalAmount }}</span>
      <el-button type="primary" @click="checkout">结算</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
// import { useStore } from '@/stores'; // 如果使用 Pinia 状态管理

// 示例数据
const router = useRouter();
const cartItems = ref([
  {
    id: 1,
    name: '商品 A',
    price: 100,
    quantity: 2
  },
  {
    id: 2,
    name: '商品 B',
    price: 150,
    quantity: 1
  },
  {
    id: 3,
    name: '商品 C',
    price: 200,
    quantity: 3
  }
]);

// const store = useStore();
// const cartItems = computed(() => store.cartItems);

const totalAmount = computed(() => {
  return cartItems.value.reduce((total, item) => total + (item.price * item.quantity), 0);
});

const removeFromCart = (itemId) => {
  cartItems.value = cartItems.value.filter(item => item.id !== itemId);
};

const updateCartItemQuantity = (itemId, quantity) => {
  const item = cartItems.value.find(item => item.id === itemId);
  if (item) {
    item.quantity = quantity;
  }
};

const checkout = () => {
  router.push('/checkout');
};
</script>

<style scoped>
.cart-page {
  padding: 20px;
}

.cart-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}
.return-button {
  margin-bottom: 20px;
}

.cart-total span {
  font-size: 18px;
  font-weight: bold;
}
</style>